;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Account.Distributormargin", [])
        .controller("DistributormarginCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "UserContextService", "$timeout", "$filter", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, UserContextService, $timeout, $filter) {
            var btnPermissionCode = {
                    distributormarginExport: window.ActionCode.distributormarginExport,
                    distributormarginAudit: window.ActionCode.distributormarginAudit,
                    distributormarginEdit: window.ActionCode.distributormarginEdit,
                    distributormarginAdd: window.ActionCode.distributormarginAdd,
                    distributormarginRemove: window.ActionCode.distributormarginRemove,
                },
                checkType = 0,
                totalReceivePrice = 0;

            $scope.btnPermission = {
                distributormarginExport: true,
                distributormarginAudit: true,
                distributormarginEdit: true,
                distributormarginAdd: true,
                distributormarginRemove: true
            };

            $scope.activeFlag = [true, false, false, false];
            $scope.sourceFlag = null;
            $scope.markInfo = {
                allChecked: false,
                curRow: -1
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
          $scope.pageInfoDetails = {
                    totalPages: 1,
                    totalRows: 0,
                    pageSize: 10,
                    curPage: 1
                };
            $scope.tableConfig = {
                fixedWidth: 160,
                fixedWidth: 190,
                scrollWidth: 1100,
                tableCol: [
                    {
                        name: "差价核算单号",
                        dataField: "billNo",
                        width: 160,
                        isShow: true,
                        isFixed: false,
                        type: "click",
                        clickFn: "viewDetail"
                    },
                    {
                        name: "客户编码",
                        dataField: "dealerCode",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "客户名称",
                        dataField: "dealerName",
                        width: 190,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "销售总价",
                        dataField: "allTotalAdjust",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "销售经销商价",
                        dataField: "allTotalSale",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "销售差价",
                        dataField: "allTotalMargin",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "创建人",
                        dataField: "createName",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "创建时间",
                        dataField: "createDateShow",
                        width: 130,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "审核人",
                        dataField: "auditName",
                        width: 100,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "审核状态",
                        dataField: "statusShow",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "operate",
                        name: "操作",
                        width: 150,
                        isShow: true,
                        isFixed: true
                    }
                ]
            };
            $scope.dataList = [];

            $scope.auditStatusList = [
                {id: 0, name: "待审核"},
                {id: 1, name: "审核通过"},
                {id: 2, name: "审核拒绝"}
            ];

            $scope.toolbarConfig = {};
            $scope.exportParam = {
                sectionUrl: ybHttp.RequestUrl("device", "/distributormargin/export"),
                sectionParam: {ids: []},
                allUrl: ybHttp.RequestUrl("device", "/distributormargin/exportAll")
            };
            $scope.condition = {checkType: checkType};

            // 收款单据详情页的返回
            $scope.goBack = function () {
                  var index = $scope.sourceFlag || 0;
                  $scope.changeTab(index);
            };

            $scope.changeTab = function (index) {
                angular.forEach($scope.activeFlag, function (activeFlag) {
                    activeFlag = false;
                });
                $scope.activeFlag[index] = true;
                switch (index) {
                    case 0:
                        initData();
                        $scope.setFixedWidth();
                         $scope.sourceFlag = null;
                        getDataList();
                        break;
                    case 1:
                    case 3:
                       $scope.sourceFlag = index;
                        break;
                    default:
                        break;
                }
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.reset = function () {
                $scope.condition = {checkType: checkType};
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                    return;
                }
                $timeout(function () {
                    $scope.toolbarConfig["show" + toolbar] = true;
                });
            };

            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            $scope.selectAll = function () {
                for (var i = 0; i < $scope.dataList.length; i++) {
                    $scope.dataList[i].isChecked = $scope.markInfo.allChecked;
                }
                getBillIds();
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.curRow = index
            };

            $scope.selectSingle = function (item) {
                var selectAll = false;
                if (!item.isChecked) {
                    selectAll = false;
                } else {
                    selectAll = checkAll($scope.dataList);
                }
                $scope.markInfo.allChecked = selectAll;
                getBillIds();
            };

            $scope.tdClick = function (item, fnType) {
                switch (fnType) {
                    case "viewDetail":
                        viewDetail(item);
                        break;
                    default:
                        break;
                }
            };

            $scope.setFixedWidth = function () {
                $scope.tbStyle = {
                    billFixedWidth: {left: $scope.tableConfig.fixedWidth + "px"}
                };
            };

            $scope.tabFlag = {};
            var tabFlag = {
                0: "新增经销商差价核算",
                1: "编辑经销商差价核算"
            };

            $scope.addCheck = function () {
                ybWidget.GetTableCol($scope.addTableInfo.tableConfig.tableCol, 'ftIsOutDesc').isShow = true;
                ybWidget.GetTableCol($scope.addTableInfo.tableConfig.tableCol, 'ftCreateName').isShow = true;

                $scope.orderObj.paymentDate = new Date();
                $scope.orderObj.operatorName = $scope.curUser.name

                $scope.saleNos = {};
                $scope.tabFlag.type = 0;
                $scope.tabFlag.name = tabFlag[0];
                $scope.changeTab(1);
            };
            $scope.editCheck = function(item) {
                ybWidget.GetTableCol($scope.addTableInfo.tableConfig.tableCol, 'ftIsOutDesc').isShow = false;
                ybWidget.GetTableCol($scope.addTableInfo.tableConfig.tableCol, 'ftCreateName').isShow = false;

                ybHttp.Get('device', '/distributormargin/item', { id: item.id }).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }

                    const data = resp.value || {};
                    $scope.saleNos = {};
                    $scope.tabFlag.type = 1;
                    $scope.tabFlag.name = tabFlag[1];
                    $scope.orderObj = data|| {};

                    const dList = data.detailVOS || [];
                    angular.forEach(dList, function(item) {
                        item.isChecked = true;
                        item.deliveryCreateDateShow = $filter("YbDate")(item.deliveryCreateDate, 'yyyy-MM-dd HH:mm:ss'); // 单据创建时间
                    })
                    $scope.addTableInfo.markInfo.allChecked = true;

                    $scope.addTableInfo.dataList = dList;

                    $scope.changeTab(1);
                });
            };


            function getBillIds() {
                $scope.exportParam.sectionParam.ids = [];
                angular.forEach($scope.dataList, function (item) {
                    if (!!item.isChecked) {
                        $scope.exportParam.sectionParam.ids.push(item.id);
                    }
                });
            }

            function viewDetail(item, isCheckFlag) {
                $scope.isCheckFlag = isCheckFlag
                $scope.tabFlagName = isCheckFlag ? "审核" : "详情"
                getDetail(item).then(function (data) {
                    if (!data) {
                        return;
                    }
                    $scope.detailObj = data;
                    $scope.detailTableInfo.dataList = data.detailVOS;
                    var df = "yyyy-MM-dd HH:mm:ss";
                    angular.forEach($scope.detailTableInfo.dataList, function (item) {
                        item.deliveryCreateDateShow = $filter("YbDate")(item.deliveryCreateDate, df);
                    });
                    $scope.changeTab(3);
                });
            }

            function checkAll(list) {
                var selectAll = false;
                for (var i = 0, len = list.length; i < len; i++) {
                    if (!list[i].isChecked) {
                        selectAll = false;
                        break;
                    }
                    selectAll = true;
                }
                return selectAll;
            }
            function getDataList(curPage) {
                $scope.markInfo.curRow = -1;
                $scope.markInfo.allChecked = false;

                var service = "device",
                    reqUrl = "/distributormargin/list",
                    param = {
                        pageSize: $scope.pageInfo.pageSize,
                        pageNum: $scope.pageInfo.curPage,
                        filters: angular.copy($scope.condition)
                    };

                if (!!curPage) {
                    param.pageNum = $scope.pageInfo.curPage = curPage;
                }
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    var df = "yyyy-MM-dd HH:mm:ss";
                    angular.forEach(resp.value.list, function (item) {
                        item.createDateShow = $filter("YbDate")(item.createDate, df);
                        item.statusShow = $filter("ProgressStatus")(item.status, "auditStatus");
                        item.isChecked = false;
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            function getDetail(item) {
                var param = {
                    id: item.id
                };
                return ybHttp.Get('device', '/distributormargin/item', param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return false;
                    }
                    return resp.value;
                });
            }

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            function initData() {
                $scope.markInfo.allChecked = false;
                $scope.markInfo.curRow = -1;
                $scope.condition = {checkType: checkType};
                $scope.dataList = [];

                $scope.orderObj = {settleType: 0};
                $scope.addTableInfo.markInfo.allChecked = false;
                $scope.addTableInfo.markInfo.curRow = -1;
                $scope.addTableInfo.dataList = [];

                $scope.detailObj = {};
                $scope.detailTableInfo.markInfo.curRow = -1;
                $scope.detailTableInfo.dataList = [];
            }

            $scope.go2checkData = function (item) {
                viewDetail(item, true)
            }
            $scope.checkData = function (id,status) {
               let param= { id: id, status:status, auditMessage:$scope.orderObj.auditMessage };
                ybHttp.Post('device', '/distributormargin/audit',param ).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return false;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    $scope.changeTab(0)
                });
            }

            $scope.printBill = function (item) {
                var id = item.id;
                var billIds = [];
                billIds.push(id);
                var param = {
                    billId: billIds,
                    printType: "Distributormargin"
                };
                ybWidget.SetIsPrintOrPreview(param);
            };

            /**
             * ***************************************** 新增 开始 *****************************************
             */
            $scope.orderObj = {settleType: 0};
            $scope.selectDealer = function () {
                var param = {
                    name: "客户",
                    type: "dealer"
                };
                ybWidget.SingleSelector(param).then(function (data) {
                    $scope.orderObj.dealerOrgId = data.dealerOrgId;
                    $scope.orderObj.dealerName = data.companyName;
                    $scope.orderObj.dealerCode = data.dealerCode;
                    getAddTableDataList();
                    clearTotalPrice();
                })
            };
            $scope.selectAgent = function () {
                var param = {
                    name: "代理商",
                    type: "dealer"
                };
                ybWidget.SingleSelector(param).then(function (data) {
                    $scope.orderObj.agentId = data.dealerOrgId;
                    $scope.orderObj.agentName = data.companyName;
                })
            };
            $scope.selectOperator = function () {
                var param = {
                    name: "经办人",
                    type: "agent",
                    supplierOrgId: $scope.curOrgId
                };
                ybWidget.SingleSelector(param).then(function (data) {
                    $scope.orderObj.operatorId = data.userId;
                    $scope.orderObj.operatorName = data.name;
                })
            };
            $scope.saveData = function (valid) {
                if (!valid) {
                    $scope.submitted = true;
                    return;
                }

                var service = "device";
                var reqUrl = "/distributormargin/add";
                if ($scope.tabFlag.type == 1) {
                    reqUrl = "/distributormargin/update";
                }

                var param = {};
                param = angular.copy($scope.orderObj);
                param.detailVOS = [];
                angular.forEach($scope.addTableInfo.dataList, function (item) {
                    if (item.isChecked) {
                        param.detailVOS.push(item);
                    }
                });

                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    $scope.changeTab(0);
                });
            };

            $scope.addTableInfo = {
                markInfo: {
                    allChecked: false,
                    curRow: -1
                },
                tableConfig: {
                    scrollWidth: 750,
                    tableCol: [
                        {
                              name: "发货通知单",
                              dataField: "deliveryNo",
                              width: 160,
                              isShow: true,
                              type: "click",
                              clickFn: "viewOrderDetail"
                        },
                        {
                            name: "销售订单",
                            dataField: "saleNo",
                            isShow: true,
                            width: 160,
                        },
                        {
                          name: "创建时间",
                          dataField: "deliveryCreateDateShow",
                          width: 160,
                          isShow: true
                        },
//                        {
//                            name: "零件编号",
//                            dataField: "materialsNo",
//                            width: 100,
//                            isShow: true
//                        },
//                        {
//                            name: "零件中文名",
//                            dataField: "materialsDes",
//                            width: 200,
//                            isShow: true
//                        },
//                        {
//                            name: "数量",
//                            dataField: "qty",
//                            width: 60,
//                            isShow: true
//                        },
//                        {
//                            name: "调整后单价",
//                            dataField: "adjustPrice",
//                            width: 150,
//                            isShow: true
//                        },
                        {
                            name: "调整后总价",
                            dataField: "totalAdjust",
                            width: 150,
                            isShow: true
                        },
                        {
                            name: "经销商价",
                            dataField: "totalSale",
                            width: 100,
                            isShow: true
                        },
                        {
                            name: "差价",
                            dataField: "totalMargin",
                            width: 60,
                            isShow: true
                        }
//                        ,

                    ]
                },
                dataList: [],
                selectRow: function (index) {
                    $scope.addTableInfo.markInfo.curRow = index
                },
                
                selectAll: function () {
                    for (var i = 0; i < $scope.addTableInfo.dataList.length; i++) {
                        $scope.addTableInfo.dataList[i].isChecked = $scope.addTableInfo.markInfo.allChecked;
                    }
                    if ($scope.addTableInfo.dataList.length > 0) {
                        countTotalPrice();
                    }
                },
                selectSingle: function (item) {
                    var selectAll = false;
                    if (!item.isChecked) {
                        selectAll = false;
                    } else {
                        selectAll = checkAll($scope.addTableInfo.dataList);
                    }
                    $scope.addTableInfo.markInfo.allChecked = selectAll;
                    countTotalPrice();
                },
                tdClick: function (item, fnType) {
                    switch (fnType) {
                        case "viewOrderDetail":
                           viewOrderDetail(item,$scope.orderObj.dealerOrgId);
                            break;
                        default:
                            break;
                    }
                }
            };

          function viewOrderDetail(item,dealerOrgId) {
                 if (!dealerOrgId) {
                        ybWidget.ShowToaster('error', '请选择客户');
                        return;
                  }
                 $scope.orderDetailTableInfo.dataList = item.detailSubVOS||[];
                 $scope.orderDetailTableInfo.deliveryNo = item.deliveryNo;
                 $scope.changeTab(2);
//                  getOrderDetail(item.saleNo,dealerOrgId).then(function (data) {
//                      if (!data) {
//                          return;
//                      }
//                      $scope.orderDetailTableInfo.dataList = data.list;
//                      $scope.orderDetailTableInfo.saleNo = item.saleNo;
//                      $scope.changeTab(2);
//                   });
                }

          // 获取发货通知单详情
          function getOrderDetail(billNo,dealerOrgId) {
            var service = "device";
            var reqUrl = "/distributormargin/listBySaleNo";
            var  param = {
                pageSize:100,
                pageNum:1,
                filters: {
                    dealerOrgId:dealerOrgId,
                    saleNo: billNo
                }
             };
                return ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return false;
                    }
                    return resp.value;
                });
            }
            function changeTableData() {
                var totalPrice = Number($scope.orderObj.totalPrice) || 0;
                angular.forEach($scope.addTableInfo.dataList, function (item) {
                    var receivePrice = Number(item.remainPrice) || 0;
                    if (totalPrice != 0) {
                        item.isChecked = true;
                        item.checkPrice = Math.min(totalPrice, receivePrice);
                        totalPrice = ybWidget.Minus(totalPrice, item.checkPrice);
                    } else {
                        item.isChecked = false;
                        item.checkPrice = null;
                    }
                });
                $scope.addTableInfo.markInfo.allChecked = checkAll($scope.addTableInfo.dataList);
            }
            //xbt
            $scope.saleNos ={};
            $scope.searchSaleNo = function () { 
                getAddTableDataList();        
            }
            function getAddTableDataList() {
                $scope.addTableInfo.markInfo.curRow = -1;
                $scope.addTableInfo.markInfo.allChecked = false;
                totalReceivePrice = 0;
                clearTotalPrice();
                var service = "device",
                    reqUrl = "/distributormargin/list4add",
                    param = {
                        pageSize: $scope.pageInfoDetails.pageSize,
                        pageNum: $scope.pageInfoDetails.curPage,
                        filters: {
                            dealerOrgId: $scope.orderObj.dealerOrgId,
                            saleNo: $scope.saleNos.saleNo
                        }
                    };

                   if (!$scope.orderObj.dealerOrgId) {
                        ybWidget.ShowToaster('error', '请选择客户');
                        return;
                       }
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                   angular.forEach(resp.value.list, function(item) {
                                    item.deliveryCreateDateShow = $filter("YbDate")(item.deliveryCreateDate, 'yyyy-MM-dd HH:mm:ss'); // 单据创建时间
                    })
                    $scope.addTableInfo.dataList = resp.value.list;
                    $scope.pageInfoDetails.totalRows = resp.value.total;
                    $scope.pageInfoDetails.totalPages = resp.value.pages;
                });
            }

            function clearTotalPrice() {
                $scope.orderObj.allTotalAdjust = "";
                $scope.orderObj.allTotalSale = "";
                $scope.orderObj.allTotalMargin = "";
            }

            function countTotalPrice() {
                var allTotalMarginSum = 0;//行差价之和
                 var totalAdjustSum = 0;//行调整后总价之和
                 var totalSaleSum = 0;   //经销商价*数量之和
                angular.forEach($scope.addTableInfo.dataList, function (item) {
                    if (item.isChecked) {
                        allTotalMarginSum = ybWidget.Plus(allTotalMarginSum, item.totalMargin);
                        totalAdjustSum = ybWidget.Plus(totalAdjustSum, item.totalAdjust);
                        totalSaleSum = ybWidget.Plus(totalSaleSum, item.totalSale);
                    }
                });
                  $scope.orderObj.allTotalMargin=allTotalMarginSum;
                  $scope.orderObj.allTotalAdjust=totalAdjustSum;
                  $scope.orderObj.allTotalSale=totalSaleSum;
            }


            function countRealPrice() {
                var totalPrice = Number($scope.orderObj.totalPrice) || 0,
                    discountPrice = Number($scope.orderObj.discountPrice) || 0;
                var advancePayPrice = Number($scope.orderObj.advancePayPrice) || 0;
                if (!!discountPrice && discountPrice > totalPrice) {
                    $scope.orderObj.discountPrice = discountPrice = totalPrice;
                    $scope.orderObj.advancePayPrice = advancePayPrice = 0;
                }
                var restPrice = ybWidget.Minus(totalPrice, discountPrice);
                if (!!advancePayPrice && advancePayPrice > restPrice) {
                    $scope.orderObj.advancePayPrice = advancePayPrice = restPrice;
                }
                var plus = ybWidget.Plus(discountPrice, advancePayPrice);
                $scope.orderObj.realPrice = ybWidget.Minus(totalPrice, plus);
            }
            /**
             * ***************************************** 新增 结束 *****************************************
             */

            /**
             * ***************************************** 详情 开始 *****************************************
             */
            $scope.detailObj = {};
            $scope.detailTableInfo = {
                markInfo: {
                    curRow: -1
                },
                tableConfig: {
                    scrollWidth: 610,
                    tableCol: [
                        {
                           name: "发货通知单",
                           dataField: "deliveryNo",
                           width: 160,
                           isShow: true,
                           type: "click",
                           clickFn: "viewOrderDetail"
                       },
                        {
                            name: "销售订单",
                            dataField: "saleNo",
                            width: 160,
                            isShow: true
                        },
                        {
                             name: "创建时间",
                             dataField: "deliveryCreateDateShow",
                             width: 160,
                             isShow: true
                        },
//                        {
//                            name: "零件编号",
//                            dataField: "materialsNo",
//                            width: 130,
//                            isShow: true
//                        },
//                        {
//                            name: "零件中文名",
//                            dataField: "materialsDes",
//                            width: 200,
//                            isShow: true
//                        },
//                        {
//                            name: "数量",
//                            dataField: "qty",
//                            width: 60,
//                            isShow: true
//                        },
//                        {
//                            name: "调整后单价",
//                            dataField: "adjustPrice",
//                            width: 150,
//                            isShow: true
//                        },
                        {
                            name: "调整后总价",
                            dataField: "totalAdjust",
                            width: 150,
                            isShow: true
                        },
                         {
                             name: "经销商价",
                             dataField: "totalSale",
                             width: 100,
                             isShow: true
                         },
                          {
                              name: "差价",
                              dataField: "totalMargin",
                              width: 60,
                              isShow: true
                          }
//                          ,

                    ]
                },
                dataList: [],
                selectRow: function (index) {
                    $scope.detailTableInfo.markInfo.curRow = index
                },
                tdClick: function (item, fnType) {
                    switch (fnType) {
                        case "viewOrderDetail":
                            viewOrderDetail(item,$scope.detailObj.dealerOrgId);
                            break;
                        default:
                            break;
                    }
                }
            };
            /**
             * ***************************************** 详情 结束 *****************************************
             */

            /**
             * ***************************************** 单据详情 开始 *****************************************
             */
            $scope.orderDetailObj = {};
            $scope.orderDetailTableInfo = {
                markInfo: {
                    curRow: -1
                },
                tableConfig: {
                    scrollWidth: 1270,
                    tableCol: [
                        {
                            name: "零件编号",
                            dataField: "materialsNo",
                            width: 150,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            name: "零件中文名",
                            dataField: "materialsDes",
                            width: 150,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            name: "数量",
                            dataField: "qty",
                            width: 80,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            name: "调整后单价",
                            dataField: "adjustPrice",
                            width: 150,
                            isShow: true,
                            isFixed: false,
                        },
//                        {
//                            name: "调整后总价",
//                            dataField: "totalAdjust",
//                            width: 150,
//                            isShow: true,
//                            isFixed: false,
//                        },
                        {
                            name: "经销商价",
                            dataField: "totalSale",
                            width: 100,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            name: "差价",
                            dataField: "totalMargin",
                            width: 100,
                            isShow: true,
                            isFixed: false,
                        }
                    ]
                },
                dataList: []
            };

            $scope.costPriceShow = false;

            // 销售出库详情table配置
            $scope.saleOutTbConfig = {
                scrollWidth: 1270,
                tableCol: [
                    {
                        type: "",
                        name: "零件编号",
                        dataField: "materialNo",
                        width: 80,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "零件中文名",
                        dataField: "materialDes",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "默认储位",
                        dataField: "defaultStorage",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "单位",
                        dataField: "unit",
                        width: 80,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "订单总数",
                        dataField: "totalOrderQty",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "已发数量",
                        dataField: "issuedQty",
                        width: 80,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "本次发货",
                        dataField: "thisShipQty",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "实际出库数量",
                        dataField: "outboundQty",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "单价",
                        dataField: "singlePrice",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        isInput: false
                    },
                    {
                        type: "",
                        name: "实际库存",
                        dataField: "realStockQty",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "意向占用",
                        dataField: "intentOccupation",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "发货占用",
                        dataField: "shipOccupation",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "安全库存",
                        dataField: "safetyStock",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "库存上线",
                        dataField: "onlineStock",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "cost",
                        name: "成本单价",
                        dataField: "costPrice",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        isInput: false
                    },
                    {
                        type: "cost",
                        name: "成本小计",
                        dataField: "costSubtotal",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        isInput: false
                    },
                ]
            };


            // 获取发货通知单详情
            function getXGFTOrderDetail(billId) {
                var service = "device";
                var reqUrl = "/deliveryNote/item";
                var param = {};
                param.id = billId;
                return ybHttp.Get(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return false;
                    }
                    return resp.value;
                });
            }

            // 获取销售出库详情
            function getXSCKOrderDetail (id) {
                var url = '/salesBillOut/item';
                return ybHttp.Get('device', url, {id: id}).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return false;
                    }
                    return res.value;
                })
            }


            $scope.delData = function (item) {
                ybWidget.ShowMessage("确定要删除吗？", MESG_TYPE.Ask).then(function (data) {
                    var url = '/distributormargin/remove';
                    ybHttp.Post('device', url, [item.id]).then(function (res) {
                        if (res.code !== 0) {
                            ybWidget.ShowMessage(res, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        $scope.search(1);
                    });
                })
            };
            /**
             * ***************************************** 单据详情 结束 *****************************************
             */

            (function () {
                $scope.setFixedWidth();
                getDataList(1);
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }])
    ;
})(window, window.angular);